<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Member\Controller\InitController;
if (!defined('MODULE_CACHE')) define('MODULE_CACHE', DATA_PATH.'caches_model/');
class ProfileController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Member');
		$this->detail_db = D('Member_detail');
		$this->sms_db = D('Sms_report');
		$this->account_db = D('Member_account');
		$this->cash_db = D('Cash');
		$this->info_db = D('Account_info');
		$this->bean_db = D('Score');
		$this->bank_db = D('Bank_type');
		$this->message_db = D('Message');
	}

	public function _empty() {
		$this->index();
	}

	/* 会员中心首页 */
	public function index() {
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$SEO=seo(0,"我的信息");
		$setting = getcache('setting', 'member');
		$coin=exchange_money($info['u_coin']);
		include template('profile');
	}
	
	/*我的站内信*/
	public function notice(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$count = $this->message_db->where(array('send_to_id'=>$this->userid))->count();
		$message_lists = $this->message_db->where(array('send_to_id'=>$this->userid))->page($pagecurr,$pagesize)->select();
		$pages = page($count,$pagesize);
		$SEO=seo(0,"我的站内信");
		include template('member_notice');
	}
	
	/*站内信内容页面*/
	public function notice_content(){
		$id = (int) I('id');
		$SEO=seo(0,"站内信内容");
		$sqlmap=array();
		$sqlmap['messageid']=array('GT',$id);
		$premap=array();
		$premap['messageid']=array('LT',$id);
		$info = $this->message_db->where(array('messageid'=>$id))->find();
		$status=$this->message_db->where(array('messageid'=>$id))->save(array('status'=>1));
		$pre_article=$this->message_db->where($premap)->find();
		$next_article=$this->message_db->where($sqlmap)->find();
		include template('member_notice_content');
	}
	
	/*设置 删除已读*/
	public function setread(){
		$act = I('act');
		$sqlMap = array();
		$sqlMap['status'] = 1;
		//全部设置已读
		if($act == 'allRead'){
			$rs = $this->message_db->where(array('status'=>0))->save($sqlMap);
		}
		//删除全部已读的信息
		if($act == 'allRemove'){
			$rs = $this->message_db->where($sqlMap)->delete();
		}
		$arr = array();
		if($rs){
			$arr = array('rs'=>1);
		}else{
			$arr = array('rs'=>0);
		}
		echo json_encode($arr);
	}
	
	//删除信息
	public function message_del(){
		$info=I('post.');
		$rs = $this->message_db->where(array('messageid'=>$info['noticeID']))->delete();
		$arr = array();
		if($rs){
			$arr = array('rs'=>1);
		}else{
			$arr = array('rs'=>0);
		}
		echo json_encode($arr);
	
	
	}
	
	
	/*绑定手机号初始化页面*/
	public function binding_tel(){
		$sms= $this->sms_db->where(array('userid'=>$this->userid))->find();
		$info=$this->db->where(array('userid'=>$this->userid))->find();
		$SEO=seo(0,"手机绑定信息");
		include template('binding_tel');
		  
	}
	
	
	
	/**/
	/*获取手机验证码*/
	public function send_telcode(){
		$user= $this->db->where(array('userid'=>$this->userid))->find();
		$userid=$user['userid'];
		$info['mobile']=$_POST['phone'];
		$info['id_code']=random(6,1);
		$info['userid']=$user['userid'];
		$info['msg']="获取手机绑定验证码为".$info['id_code'];
		$info['status']=$user['status'];
		$info['posttime']=time();
		$info['enum']='tel';
		$info['ip']=$user['regip']; 
		                    
		$mobile=$_POST['phone'];
		if (!is_mobile($mobile)){
		$data['flag']=0;
		$data['msg']="请输入有效电话号码！";
		echo json_encode($data);
		return ;
		}
		//验证一个手机是否只能绑定且认证一个账号
		$phone=$this->db->where(array('mobile'=>$mobile))->find();
		if ($phone){
			$data['flag']=0;
			$data['msg']="号码已绑定过，请重新输入！";
			echo json_encode($data);
		}else{
		
		//查询当天发送验证码是否查过三次
		$timetoday = strtotime ( date ( "Y-m-d", time () ) ); // 今天0点的时间点
	    $time2 = $timetoday + 3600 * 24;
		$sql="select count(*) as counts from tp_sms_report where enum='tel' and mobile=$mobile and posttime >= '$timetoday' and  posttime<'$time2'";
		$count=$this->sms_db->query($sql);
		foreach ($count as $v){
			$counts=$v['counts'];
		}
		if ($counts>3){
			$data['flag']=0;
			$data['msg']="你已发送了三条,请改天在验证！";
			$data['data']=$counts;
			echo json_encode($data);
		}else {
		$send=$this->sms_db->add($info);
		//发送手机短信验证码
		$sms = new \Sms\Api\SmsApi();
		$send_code=$sms->send($mobile,$info['msg']);
			if (!$send_code){
				$data['flag']=0;
				$data['msg']="发送失败！";
				echo json_encode($data);
			}else{
				$add_mobile=$this->db->where(array('userid'=>$this->userid))->save(array('mobile'=>$mobile));
				if ($add_mobile){
				$data['flag']=1;
				$data['msg']="发送成功！";
				echo json_encode($data);
				}
			}
		
		}
		
		}
		
	}
	/*绑定手机*/	
	public function binding(){
		$info=I('post.');
		$mobile=$info['mobile'];
		$code=$info['id_code'];
		$user= $this->sms_db->where(array('mobile'=>$mobile,'id_code'=>$code))->find();
		$userid=$user['userid'];
	  	$time=time()-$user['posttime'];
	  	
		if ($time>600){
			$data['flag']=0;
			$data['msg']="验证码已失效！";
			echo json_encode($data);
		}else{
		if (!$user){
			$data['flag']=0;
			$data['msg']="绑定失败！";
			echo json_encode($data);
		}else{
			$tel=array();
			$tel['phone_status'] = 1;
			$tel['userid'] = $userid;
			$result=$this->db->update($tel);
			if ($result){
			$data['flag']=1;
			$data['msg']="绑定成功！";
			echo json_encode($data);
			}
			
		}
		
		}
		
	}
	/*详细资料*/
	public function detail_profile() {
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$detail=$this->detail_db->where(array('userid'=>$this->userid))->find();
		extract($info);
		$SEO=seo(0,"详细资料");
		include template('person_profile');
	}
	/*编辑详细资料*/
	public function profile_edit(){
		$info=I('post.');
		$result=$this->db->profile_info($info);
		if (!$result) {
			$data ['flag'] = 0;
			$data ['msg'] =$this->db->getError();
			echo json_encode($data);
		}else{
			$data ['flag'] =1;
			$data ['msg'] ='更新资料成功！';
			echo json_encode($data);
		}
	}
	/*邮箱认证*/
	public function attest(){
		$SEO=seo(0,"邮箱认证信息");
		$info=$this->db->where(array('userid'=>$this->userid))->find();
		include template('attest');;
	}
	
	/*发送邮件*/
	public function send_email(){
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$userid=$info['userid'];
		$email=$info['email'];
		if ($email){
		 helpers('mail');
		 $settings = getcache('setting','member');
		 $code = authcode($userid.'|'.md5(C('AUTHKEY')), 'ENCODE');
         $url = U('Member/Index/verify_email', array('code' => $code), '', TRUE);
         $message = $settings['registerverifymessage'];
         $arr = array('{webname}'  => C('webname'),'{username}' => $info['username'], '{email}' => $info['email'], '{url}' => $url,'{siteurl}'  => HTTP_HOST
            );
         $message = str_replace(array_keys($arr), $arr, $message);
		 sendmail($email, '邮箱认证', $message);
		}
		$SEO=seo(0,"发送邮件信息");
		include template('send_email');
	}
	
	/*初始化修改密码页面*/
	public function password(){
		$SEO=seo(0,"修改密码");
		include template('password');
	}
	/*修改密码验证密码是否为登陆密码*/
	public function password_edit(){
		$user=$this->db->where(array('userid'=>$this->userid))->find();
		$info=I('post.');
		$password = htmlspecialchars($info['pwd']);
    //echo md5(md5($password.$user['encrypt']));
		$newpwd=htmlspecialchars($info['newpwd']);
		$cmpwd=htmlspecialchars($info['cmpwd']);
		
		if($user['password']!= md5(md5($password.$user['encrypt']))){
			$data ['flag'] =0;
			$data ['msg'] ='你输入的密码有误！';
			echo json_encode($data);
		}else{
			$data ['flag'] =1;
			$data ['msg'] ='输入正确！';
			echo json_encode($data);
		
		}		
		
	}
	
	/*修改密码*/
	public function pwd_info(){
		$user=$this->db->where(array('userid'=>$this->userid))->find();
		$info=I('post.');
		$newpwd=htmlspecialchars($info['newpwd']);
		$cmpwd=htmlspecialchars($info['cmpwd']);
		$pwd=md5(md5($newpwd.$user['encrypt']));
		
		$code=$info['coder'];
		if($newpwd!=$cmpwd){
			$data ['flag'] =0;
			$data ['msg'] ='你输入的密码不一致！';
			echo json_encode($data);
		}
		$sms=$this->sms_db->where(array('userid'=>$this->userid,'id_code'=>$code))->find();
		if ($sms){
		 $result=$this->db->where(array('userid'=>$this->userid))->save(array('password'=>$pwd));
			if ($result){
			$data ['flag'] =1;
			$data ['msg'] ='更改密码成功！';
			echo json_encode($data);
			}
		}
		
	}

	
	
	
	
	

	/*获取密码code*/
	public function pwd_code(){
		$sms=A("Member/Sms");
		$sms->code("修改密码的验证码为：","pwd");
	}
	
	
	
	/*修改头像初始化页面*/
	public function  portrait(){
		$SEO=seo(0,"修改头像信息");
		include template('photo');
	}
	/*修改头像*/
	public function edit_photo(){
		$info=I('post.');
		$id=$info['pid'];
		$reuslt=$this->detail_db->where(array('userid'=>$this->userid))->save(array('photo'=>$id));
		if ($reuslt) {
			$data ['flag'] =1;
			$data ['msg'] ='修改头像成功！';
			echo json_encode($data);
		}
	}
	
	
	
	
	

}